package com.itheima.web;

import com.itheima.cst.SysConst;
import com.itheima.entity.Result;
import com.itheima.utils.FileUtils;
import com.itheima.utils.RedisUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class UploadController {

//    @Resource
//    RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String path = FileUtils.upload(file.getInputStream(),file.getOriginalFilename());
        //把文件路径保存到redis
        RedisUtil.addToSet(SysConst.FILE_ALL_SET_KEY,path);
//        redisTemplate.opsForSet().add("file:allset",path);
        //redis命令 set key value
        //expire key seconds
//        redisTemplate.opsForValue().set("file:" + path,1,1, TimeUnit.MINUTES);
        RedisUtil.set(SysConst.FILE_BASE_KEY + path,0,SysConst.FILE_TIMEOUT, TimeUnit.MINUTES);
        return new Result(path);
    }

}
